/*     */ package com.cr.system.service.impl;
/*     */ 
/*     */ import com.cr.common.annotation.DataScope;
/*     */ import com.cr.common.core.domain.entity.SysRole;
/*     */ import com.cr.common.core.domain.entity.SysUser;
/*     */ import com.cr.common.core.text.Convert;
/*     */ import com.cr.common.exception.ServiceException;
/*     */ import com.cr.common.utils.ShiroUtils;
/*     */ import com.cr.common.utils.MyStringUtils;
/*     */ import com.cr.common.utils.spring.SpringUtils;
/*     */ import com.cr.system.domain.SysRoleDept;
/*     */ import com.cr.system.domain.SysRoleMenu;
/*     */ import com.cr.system.domain.SysUserRole;
/*     */ import com.cr.system.mapper.SysRoleDeptMapper;
/*     */ import com.cr.system.mapper.SysRoleMapper;
/*     */ import com.cr.system.mapper.SysRoleMenuMapper;
/*     */ import com.cr.system.mapper.SysUserRoleMapper;
/*     */ import com.cr.system.service.ISysRoleService;
/*     */ import java.util.ArrayList;
/*     */ import java.util.Arrays;
/*     */ import java.util.HashSet;
/*     */ import java.util.List;
/*     */ import java.util.Set;
/*     */ import org.springframework.beans.factory.annotation.Autowired;
/*     */ import org.springframework.stereotype.Service;
/*     */ import org.springframework.transaction.annotation.Transactional;
/*     */ 
/*     */ @Service
/*     */ public class SysRoleServiceImpl implements ISysRoleService {
/*     */   @Autowired
/*     */   private SysRoleMapper roleMapper;
/*     */   
/*     */   @Autowired
/*     */   private SysRoleMenuMapper roleMenuMapper;
/*     */   
/*     */   @Autowired
/*     */   private SysUserRoleMapper userRoleMapper;
/*     */   
/*     */   @Autowired
/*     */   private SysRoleDeptMapper roleDeptMapper;
/*     */   
/*     */   @DataScope(deptAlias = "d")
/*     */   public List<SysRole> selectRoleList(SysRole role) {
/*  59 */     return this.roleMapper.selectRoleList(role);
/*     */   }
/*     */   
/*     */   public Set<String> selectRoleKeys(Long userId) {
/*  71 */     List<SysRole> perms = this.roleMapper.selectRolesByUserId(userId);
/*  72 */     Set<String> permsSet = new HashSet<>();
/*  73 */     for (SysRole perm : perms) {
/*  75 */       if (MyStringUtils.isNotNull(perm))
/*  77 */         permsSet.addAll(Arrays.asList(perm.getRoleKey().trim().split(","))); 
/*     */     } 
/*  80 */     return permsSet;
/*     */   }
/*     */   
/*     */   public List<SysRole> selectRolesByUserId(Long userId) {
/*  92 */     List<SysRole> userRoles = this.roleMapper.selectRolesByUserId(userId);
/*  93 */     List<SysRole> roles = selectRoleAll();
/*  94 */     for (SysRole role : roles) {
/*  96 */       for (SysRole userRole : userRoles) {
/*  98 */         if (role.getRoleId().longValue() == userRole.getRoleId().longValue())
/* 100 */           role.setFlag(true); 
/*     */       } 
/*     */     } 
/* 105 */     return roles;
/*     */   }
/*     */   
/*     */   public List<SysRole> selectRoleAll() {
/* 116 */     return ((SysRoleServiceImpl)SpringUtils.getAopProxy(this)).selectRoleList(new SysRole());
/*     */   }
/*     */   
/*     */   public SysRole selectRoleById(Long roleId) {
/* 128 */     return this.roleMapper.selectRoleById(roleId);
/*     */   }
/*     */   
/*     */   @Transactional
/*     */   public boolean deleteRoleById(Long roleId) {
/* 142 */     this.roleMenuMapper.deleteRoleMenuByRoleId(roleId);
/* 144 */     this.roleDeptMapper.deleteRoleDeptByRoleId(roleId);
/* 145 */     return (this.roleMapper.deleteRoleById(roleId) > 0);
/*     */   }
/*     */   
/*     */   @Transactional
/*     */   public int deleteRoleByIds(String ids) {
/* 158 */     Long[] roleIds = Convert.toLongArray(ids);
/* 159 */     for (Long roleId : roleIds) {
/* 161 */       checkRoleAllowed(new SysRole(roleId));
/* 162 */       SysRole role = selectRoleById(roleId);
/* 163 */       if (countUserRoleByRoleId(roleId) > 0)
/* 165 */         throw new ServiceException(String.format("%1$s已分配,不能删除", new Object[] { role.getRoleName() })); 
/*     */     } 
/* 169 */     this.roleMenuMapper.deleteRoleMenu(roleIds);
/* 171 */     this.roleDeptMapper.deleteRoleDept(roleIds);
/* 172 */     return this.roleMapper.deleteRoleByIds(roleIds);
/*     */   }
/*     */   
/*     */   @Transactional
/*     */   public int insertRole(SysRole role) {
/* 186 */     this.roleMapper.insertRole(role);
/* 187 */     return insertRoleMenu(role);
/*     */   }
/*     */   
/*     */   @Transactional
/*     */   public int updateRole(SysRole role) {
/* 201 */     this.roleMapper.updateRole(role);
/* 203 */     this.roleMenuMapper.deleteRoleMenuByRoleId(role.getRoleId());
/* 204 */     return insertRoleMenu(role);
/*     */   }
/*     */   
/*     */   @Transactional
/*     */   public int authDataScope(SysRole role) {
/* 218 */     this.roleMapper.updateRole(role);
/* 220 */     this.roleDeptMapper.deleteRoleDeptByRoleId(role.getRoleId());
/* 222 */     return insertRoleDept(role);
/*     */   }
/*     */   
/*     */   public int insertRoleMenu(SysRole role) {
/* 232 */     int rows = 1;
/* 234 */     List<SysRoleMenu> list = new ArrayList<>();
/* 235 */     for (Long menuId : role.getMenuIds()) {
/* 237 */       SysRoleMenu rm = new SysRoleMenu();
/* 238 */       rm.setRoleId(role.getRoleId());
/* 239 */       rm.setMenuId(menuId);
/* 240 */       list.add(rm);
/*     */     } 
/* 242 */     if (list.size() > 0)
/* 244 */       rows = this.roleMenuMapper.batchRoleMenu(list); 
/* 246 */     return rows;
/*     */   }
/*     */   
/*     */   public int insertRoleDept(SysRole role) {
/* 256 */     int rows = 1;
/* 258 */     List<SysRoleDept> list = new ArrayList<>();
/* 259 */     for (Long deptId : role.getDeptIds()) {
/* 261 */       SysRoleDept rd = new SysRoleDept();
/* 262 */       rd.setRoleId(role.getRoleId());
/* 263 */       rd.setDeptId(deptId);
/* 264 */       list.add(rd);
/*     */     } 
/* 266 */     if (list.size() > 0)
/* 268 */       rows = this.roleDeptMapper.batchRoleDept(list); 
/* 270 */     return rows;
/*     */   }
/*     */   
/*     */   public String checkRoleNameUnique(SysRole role) {
/* 282 */     Long roleId = Long.valueOf(MyStringUtils.isNull(role.getRoleId()) ? -1L : role.getRoleId().longValue());
/* 283 */     SysRole info = this.roleMapper.checkRoleNameUnique(role.getRoleName());
/* 284 */     if (MyStringUtils.isNotNull(info) && info.getRoleId().longValue() != roleId.longValue())
/* 286 */       return "1"; 
/* 288 */     return "0";
/*     */   }
/*     */   
/*     */   public String checkRoleKeyUnique(SysRole role) {
/* 300 */     Long roleId = Long.valueOf(MyStringUtils.isNull(role.getRoleId()) ? -1L : role.getRoleId().longValue());
/* 301 */     SysRole info = this.roleMapper.checkRoleKeyUnique(role.getRoleKey());
/* 302 */     if (MyStringUtils.isNotNull(info) && info.getRoleId().longValue() != roleId.longValue())
/* 304 */       return "1"; 
/* 306 */     return "0";
/*     */   }
/*     */   
/*     */   public void checkRoleAllowed(SysRole role) {
/* 317 */     if (MyStringUtils.isNotNull(role.getRoleId()) && role.isAdmin())
/* 319 */       throw new ServiceException("不允许操作超级管理员角色"); 
/*     */   }
/*     */   
/*     */   public void checkRoleDataScope(Long roleId) {
/* 331 */     if (!SysUser.isAdmin(ShiroUtils.getUserId())) {
/* 333 */       SysRole role = new SysRole();
/* 334 */       role.setRoleId(roleId);
/* 335 */       List<SysRole> roles = ((SysRoleServiceImpl)SpringUtils.getAopProxy(this)).selectRoleList(role);
/* 336 */       if (MyStringUtils.isEmpty(roles))
/* 338 */         throw new ServiceException("没有权限访问角色数据！"); 
/*     */     } 
/*     */   }
/*     */   
/*     */   public int countUserRoleByRoleId(Long roleId) {
/* 352 */     return this.userRoleMapper.countUserRoleByRoleId(roleId);
/*     */   }
/*     */   
/*     */   public int changeStatus(SysRole role) {
/* 364 */     return this.roleMapper.updateRole(role);
/*     */   }
/*     */   
/*     */   public int deleteAuthUser(SysUserRole userRole) {
/* 376 */     return this.userRoleMapper.deleteUserRoleInfo(userRole);
/*     */   }
/*     */   
/*     */   public int deleteAuthUsers(Long roleId, String userIds) {
/* 389 */     return this.userRoleMapper.deleteUserRoleInfos(roleId, Convert.toLongArray(userIds));
/*     */   }
/*     */   
/*     */   public int insertAuthUsers(Long roleId, String userIds) {
/* 402 */     Long[] users = Convert.toLongArray(userIds);
/* 404 */     List<SysUserRole> list = new ArrayList<>();
/* 405 */     for (Long userId : users) {
/* 407 */       SysUserRole ur = new SysUserRole();
/* 408 */       ur.setUserId(userId);
/* 409 */       ur.setRoleId(roleId);
/* 410 */       list.add(ur);
/*     */     } 
/* 412 */     return this.userRoleMapper.batchUserRole(list);
/*     */   }
/*     */ }


/* Location:              D:\tmp\creditorRights-admin\BOOT-INF\lib\creditorRights-system-4.7.0.jar!\com\cr\system\service\impl\SysRoleServiceImpl.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */